package com.qsd.common.handler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import cn.hutool.json.JSONUtil;

/**
 * @Description 
 * @Author Esion
 * @Data 2020年7月3日
 */

public class MapToString implements TypeHandler<Map<String, Object>> {

	@Override
	public void setParameter(PreparedStatement ps, int i, Map<String, Object> parameter, JdbcType jdbcType)
			throws SQLException {
		String content = JSONUtil.toJsonStr(parameter);
		ps.setString(i, content);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getResult(ResultSet rs, String columnName) throws SQLException {
		String content = rs.getString(columnName);
		return JSONUtil.toBean(content, Map.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getResult(ResultSet rs, int columnIndex) throws SQLException {
		String content = rs.getString(columnIndex);
		return JSONUtil.toBean(content, Map.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getResult(CallableStatement cs, int columnIndex) throws SQLException {
		String content = cs.getString(columnIndex);
		return JSONUtil.toBean(content, Map.class);
	}


}
